package com.nbjtjc.safe.activiti.api;

import com.nbjtjc.common.validator.ValidatorUtils;
import com.nbjtjc.common.validator.group.UpdateGroup;
import com.nbjtjc.safe.activiti.model.ExtActNode;
import com.nbjtjc.safe.activiti.model.ExtActTaskReassign;
import com.nbjtjc.safe.activiti.service.ActivitiService;
import com.nbjtjc.safe.activiti.service.ExtActTaskReassignService;
import com.nbjtjc.safe.bean.Result;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/3/27 14:38
 * 工作流部署删除的主要接口
 **/
@RestController
@RequestMapping("/activiti")
@CrossOrigin("*")
public class ActivitiController {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ActivitiService activitiService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private ExtActTaskReassignService extActTaskReassignService;
    @GetMapping("/getDeploymentFile")
    public Result getDeploymentFile(HttpServletResponse response){
        Result result=new Result();
        result=activitiService.getDeploymentBpmnFile(response);
        return result;
    }
    /**
     * 部署流程
     * @param request
     * @param name
     * @return
     */
    @PostMapping("/deployment")
    public Result deployment(HttpServletRequest request,String name){
        Result result=new Result();
        try {
            // 解析器解析request的上下文未登录或者过期
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            // 先判断request中是否包含mutipart类型的数据
            if (multipartResolver.isMultipart(request)) {
                // 再将request中得数据转化成multipart类型的数据f
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                List<MultipartFile> multipartFiles = multiRequest.getFiles("file");
                Iterator iterator = multiRequest.getFileNames();
                while (iterator.hasNext()) {
                    MultipartFile file = multiRequest.getFile((String) iterator.next());
                   result=activitiService.deploymentProcess(file,name);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("部署失败" + e.getMessage());
        }
        return result;
    }

    /**
     * 获取已经部署的流程
     * @return
     */
    @GetMapping("/getDeployedProcess")
    public Result getDeployProcess(){
        Result result=activitiService.getDeploymentProcess();
        return result;
    }

    /**
     * 获取流程的所有可配置节点的定义
     * @param processDefId
     * @return
     */
    @GetMapping("/getProcessNode/{processDefId}")
    public Result getProcessNode(@PathVariable("processDefId") String processDefId){
        Result result =activitiService.getProcessNodes(processDefId);
        return result;
    }

    /**
     * 更新节点定义
     * @param extActNode
     * @return
     */
    @PostMapping("/updateProcessNode")
    public Result updateProcessNode(ExtActNode extActNode){
        ValidatorUtils.validateEntity(extActNode,UpdateGroup.class);
        Result result=activitiService.updateProcessNode(extActNode);
        return result;
    }

    /**
     * 获取流程定义的图片
     * @param response
     * @param processDefId
     * @throws IOException
     */
    @GetMapping("/getProcessPic/{processDefId}")
    public void getProcessPic(HttpServletResponse response,@PathVariable("processDefId") String processDefId) throws IOException {
        activitiService.getProcessPic(response,processDefId);
    }

    /**
     * 获取流程历史执行任务操作
     * @param processInstanceId
     * @return
     */
    @GetMapping("/getProcessHistoryTask/{processInstanceId}")
    public Result getProcessHistoryTask(@PathVariable("processInstanceId") String processInstanceId){
        Result result=new Result();
        result.setData(activitiService.getHistoryTask(processInstanceId));
        return result;
    }
    /**
     * 获取流程运行当中的流程图
     * @param response
     * @param processInstanceId
     */
    @GetMapping("/getRunningProcessPic/{processInstanceId}")
    public void getRunningProcessPic(HttpServletResponse response,@PathVariable("processInstanceId") String processInstanceId) throws Exception {
        activitiService.getRunningProcessPicByInstantId(response,processInstanceId);
    }

    @PostMapping("/deleteProcessInstance")
    public Result deleteProcessInstance(String procInstanceId){
        Result result=new Result();
        try {
//            runtimeService.deleteProcessInstance(procInstanceId,MyShiroUtils.getSessionUserName()+"删除流程");
            activitiService.deleteProcessInstance(procInstanceId);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("删除流程失败");
        }
        return result;
    }

    @GetMapping("/assignTask/{taskId}/{userId}")
    public Result assignTask(@PathVariable("taskId") String taskId,@PathVariable("userId") Integer userId){
        Result result=new Result();
        taskService.setAssignee(taskId,userId.toString());
        return result;
    }

    @PostMapping("/reassignTask")
    public Result reassignTask(ExtActTaskReassign extActTaskReassign){
        Result result=new Result();
        result=extActTaskReassignService.reassignTask(extActTaskReassign);
        return result;
    }

    @PostMapping("/reassignReturn")
    public Result reassignReturn(ExtActTaskReassign extActTaskReassign){
        Result result=new Result();
        result=extActTaskReassignService.reassignReturn(extActTaskReassign);
        return result;
    }
}
